package com.foo.designpattern.interpreter;

/**
 * com.foo.designpattern.interpreter
 * Created with IntelliJ IDEA.
 * Created by jiang on 2016-06-12 15:00.
 *
 * @Description 非终结符表达式(Non Terminal Expression)角色--Or
 * 代表逻辑"或"操作的Or类，代表由两个布尔表达式通过逻辑"或"操作给出一个新的布尔表达式的操作。
 */

public class Or implements Expression {
    private Expression left, right;

    public Or(Expression left, Expression right) {
        this.left = left;
        this.right = right;
    }

    @Override
    public boolean interpret(Context ctx) {
        return this.left.interpret(ctx) || this.right.interpret(ctx);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj != null && obj instanceof Or) {
            return this.left.equals(((Or) obj).left) && this.right.equals(((Or) obj).right);
        }
        return false;
    }

    @Override
    public int hashCode() {
        return this.toString().hashCode();
    }

    @Override
    public String toString() {
        return "(" + this.left.toString() + " OR " + this.right.toString() + ")";
    }
}
